package com.xss.okhttptest;

import android.app.Activity;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.xss.okhttptest.okhttp.CallBackUtil;
import com.xss.okhttptest.okhttp.OkhttpUtil;
import com.xss.okhttptest.pgisokhttp.OkHttpClientManager;

import java.io.File;

import okhttp3.Call;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private ImageView imageView;
    private Activity mainActivity;
    private static final String URL="http://www.wanandroid.com/article/list/0/json";
    private static final String URLBITMAP="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1537181746416&di=7287ae5e36649d354fb411ed97369b5e&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2" +
            "Fpic%2Fitem%2F3bf33a87e950352a0b8897355843fbf2b2118b26.jpg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainActivity=this;
        initView();
        getData();
    }

    private void getData() {
//        OkHttpClientManager.getInstance().get(URL, new OkHttpClientManager.HttpCallback() {
//            @Override
//            public void onSuccess(String data) {
//                textView.setText(data);
//            }
//
//            @Override
//            public void onError(String msg) {
//                Toast.makeText(mainActivity,msg,Toast.LENGTH_SHORT).show();
//            }
//        });


        OkhttpUtil.okHttpGet(URL, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                textView.setText(response);
            }
        });

        OkhttpUtil.okHttpGet(URLBITMAP, new CallBackUtil.CallBackBitmap() {
            @Override
            public void onFailure(Call call, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Bitmap response) {
                Log.d("xss", "onResponse: have bitmap");
                imageView.setImageBitmap(response);
            }
        });








        Glide.with(this).load(URLBITMAP).into(imageView);



    }

    private void initView() {
        textView=findViewById(R.id.text);
        imageView=findViewById(R.id.image);
    }
}
